home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 49 / Amiga Format CD49 (2000-01-17)(Future Publishing)(GB)(Track 1 of 3)[!][issue 2000-02].iso / -serious- / misc / football / exec / cupscheduler.rexx < prev    next >
OS/2 REXX Batch file  |  1999-11-29  |  13KB  |  497 lines

  1. /* ***********************************************************************
  2.  
  3.    CUP SCHEDULER PROGRAM FOR FOOTBALL REXX SUITE
  4.   -----------------------------------------------
  5.                    Copyright  Mark Naughton 1997
  6.  
  7.  
  8. Version    Date     History
  9. --------------------------------------------------------------------------
  10.  1.0       121097   Wrote the random match generator.
  11.            201097   Wrote the main routine that creates all the files,
  12.                     such as sorting the 'cf' file, creating the
  13.                     working/history files and randomly generating a
  14.                     schedule. This program will ONLY create the files and
  15.                     not maintain them. That is left to
  16.                     "CupScheduleNextRound.rexx".
  17.            221097   Tidied report and added number of matches to be
  18.                     played. If a history file is found but it is switched
  19.                     off then it will not be deleted.
  20.            311097   Changed format of 'working' file - added CRDN.
  21.            091297   Cup history file creation has been dummied out for
  22.                     the time being.
  23.            151297   Tidied display and removed CupHistory code.
  24.            060698   Added code for manual scheduling.
  25.            260898   Added code for handling 2 legs.
  26.            050998   Didn't read setting for away goals.
  27.            250899   Added error msg for file check.
  28.            290899   Converted to use locale. Some error messages, before
  29.                     reading the locale, will still be in English.
  30.            030999   Moved global Cup variables into a different file.
  31.            040999   Report shows better words for the settings for the
  32.                     rounds - Have decided to keep the datafile settings in
  33.                     English for easier controls - looks better though....
  34.  
  35. **************************************************************************
  36.  
  37. Procedure
  38. ---------
  39.  
  40. 1. Check files exist.
  41. 2. Read '.cf' and write teams to output file.
  42. 3. Using external program, sort the teams.
  43. 4. Re-read the teams and recreate '.cf'.
  44. 5. Open '.cf' for reading.
  45. 6. Extract data and store.
  46. 7. If no separator in it, then take it as a
  47.    team. Strip blanks and store.
  48. 8. Close input file.
  49. 9. Create '.cfrw' working file with round/teams.
  50. 10.Randomly generate who is going to play who and store in array but only
  51.    if the matches are to be automatically scheduled.
  52. 11.Create '.scf' file and using the order in which the numbers were
  53.    generated earlier, write the particular teams.
  54. 12.Write report and exit.
  55.  
  56. ************************************************************************** */
  57. PARSE ARG league_stuff
  58.  
  59. version      = 1
  60. input_file   = '.cf'
  61. input2_file  = '.scf'
  62. input3_file  = '.cfrw'
  63. input4_file  = '.cfh'
  64. output3_file = '.temp'
  65. title        = '*CUP_TITLE='
  66. thirdpl      = '*CUP_THIRD='
  67. rounddef     = '*CUP_RNDDEF='
  68. cuphistory   = '*CUP_HISTORY='
  69. schedtype    = '*CUP_SCHEDULE='
  70. awaygs       = '*CUP_AWAYG='
  71. pkversion    = '*  Version='
  72. matches.     = '???'
  73. separator    = '*'
  74. teams.       = '???'
  75. rnds.        = '???'
  76. tcount       = 0
  77. ttc          = 0
  78. rndcnt       = 0
  79. league_file  = "Data/"league_stuff
  80.  
  81. if open(datafile,"Data/Football.locale",'r') then do
  82.    line = readln(datafile)
  83.    locdir = strip(line)
  84.    close(datafile)
  85. end
  86. else do
  87.    say
  88.    say "ERROR :    (CupScheduler)"
  89.    say
  90.    say "Cannot read 'Data/Football.locale' for the locale settings."
  91.    exit
  92. end
  93.  
  94. dcupdir = locdir"Football.locale_cup"
  95. locdir  = locdir"Exec/CupScheduler.data"
  96.  
  97. if open(datafile,"ENV:FootballRXPath",'r') then do
  98.    line = readln(datafile)
  99.    rxdir = strip(line)
  100.    close(datafile)
  101. end
  102. else
  103.    rxdir = "SYS:Rexxc/"
  104.  
  105. if exists(locdir) > 0 then do
  106.   address command rxdir'rx 'locdir
  107.   VarCount = getclip('VarCount')
  108.   do i = 1 to VarCount
  109.     interpret getclip('var.'i)
  110.   end
  111. end
  112. else do
  113.    say
  114.    say "ERROR :    (CupScheduler)"
  115.    say
  116.    say "Cannot find '"locdir"' to read locale settings."
  117.    exit
  118. end
  119.  
  120. if exists(dcupdir) > 0 then do
  121.   address command rxdir'rx 'dcupdir
  122.   VarCount = getclip('VarCount')
  123.   do i = 1 to VarCount
  124.     interpret getclip('var.'i)
  125.   end
  126. end
  127. else do
  128.    say
  129.    say "ERROR :    (CupScheduler)"
  130.    say
  131.    say "Cannot find '"dcupdir"' to read cup locale settings."
  132.    exit
  133. end
  134.  
  135. if exists(league_file || input_file) = 0 then do
  136.    say
  137.    say cs_error
  138.    say
  139.    say cs_t1"'"league_file||input_file"'."
  140.    exit
  141. end
  142.  
  143. ttc = 0
  144. tcount = 0
  145. if open(datafile,league_file || input_file,'r') then do
  146.    if open(datafile2,league_file || output3_file,'w') then do
  147.       do while ~eof(datafile)
  148.          line = readln(datafile)
  149.          line = strip(line)
  150.          if pos(separator,line) > 0 then do
  151.             tcount = tcount + 1
  152.             teams.tcount = line
  153.          end
  154.          else do
  155.             ttc = ttc + 1
  156.             writeln(datafile2,line)
  157.          end
  158.       end
  159.       close(datafile2)
  160.    end
  161.    else do
  162.      say
  163.      say cs_error
  164.      say
  165.      say cs_t2"'"league_file||output3_file"'."
  166.      close(datafile)
  167.      exit
  168.    end
  169.    close(datafile)
  170. end
  171. else do
  172.    say
  173.    say cs_error
  174.    say
  175.    say cs_t3"'"league_file || input_file"'"cs_t4
  176.    exit
  177. end
  178.  
  179. address command 'sort 'league_file || output3_file league_file || output3_file
  180.  
  181. if open(datafile2,league_file || output3_file,'r') then do
  182.    if open(datafile,league_file || input_file,'w') then do
  183.       do i=1 to tcount
  184.          writeln(datafile,teams.i)
  185.       end
  186.       do j=1 to ttc                   /* while ~eof(datafile2)  */
  187.          line = readln(datafile2)
  188.          line = strip(line)
  189.          if j = ttc then
  190.             writech(datafile,line)
  191.          else
  192.             writeln(datafile,line)
  193.       end
  194.       close(datafile)
  195.    end
  196.    else do
  197.      say
  198.      say cs_error
  199.      say
  200.      say cs_t2"'"league_file||input_file"'."
  201.      close(datafile2)
  202.      exit
  203.    end
  204.    close(datafile2)
  205. end
  206. else do
  207.    say
  208.    say cs_error
  209.    say
  210.    say cs_"'"league_file||output3_file"'."
  211.    close(datafile)
  212.    exit
  213. end
  214.  
  215. address command 'delete >NIL: 'league_file || output3_file
  216.  
  217. tcount = 0
  218. if open(datafile,league_file || input_file,'r') then do
  219.    do while ~eof(datafile)
  220.       line = readln(datafile)
  221.       if pos(title,line) > 0 then        cupname  = delstr(line,1,11)
  222.       if pos(thirdpl,line) > 0 then      third    = delstr(line,1,11)
  223.       if pos(awaygs,line) > 0 then       awaygoals= delstr(line,1,11)
  224.       if pos(pkversion,line) > 0 then    fversion = delstr(line,1,12)
  225.       if pos(cuphistory,line) > 0 then   cuph     = delstr(line,1,13)
  226.       if pos(schedtype,line) > 0 then    scht     = delstr(line,1,14)
  227.       if pos(rounddef,line) > 0 then do
  228.          rndcnt = rndcnt + 1
  229.          rnds.rndcnt = delstr(line,1,12)
  230.       end
  231.       if pos(separator,line) = 0 then do
  232.          line = strip(line)
  233.          tcount       = tcount + 1
  234.          teams.tcount = line
  235.       end
  236.    end
  237.    close(datafile)
  238. end
  239. else do
  240.    say
  241.    say cs_error
  242.    say
  243.    say cs_t3"'"league_file || input_file"'."
  244.    exit
  245. end
  246.  
  247. if open(datafile2,league_file || input3_file,'w') then do
  248.    writeln(datafile2,"*")
  249.    writeln(datafile2,"* WORKING CUP FILE")
  250.    writeln(datafile2,"*")
  251.    writeln(datafile2,"*CUP_TITLE="cupname)
  252.    writeln(datafile2,"*CUP_ROND=1")
  253.    if pos("2 Legs",rnds.1) > 0 then
  254.       writeln(datafile2,"*CUP_CRDN="left(rnds.1,6)"  1 Leg")
  255.    else
  256.       writeln(datafile2,"*CUP_CRDN="left(rnds.1,6))
  257.    writeln(datafile2,"*CUP_RDEF="rnds.1)
  258.    writeln(datafile2,"*CUP_TCNT="tcount)
  259.    writeln(datafile2,"*")
  260.    do i=1 to tcount
  261.       if i = tcount then
  262.          writech(datafile2,teams.i)
  263.       else
  264.          writeln(datafile2,teams.i)
  265.    end
  266.    close(datafile2)
  267. end
  268. else do
  269.    say
  270.    say cs_error
  271.    say
  272.    say cs_t2"'"league_file || input3_file"'."
  273.    exit
  274. end
  275.  
  276. /* Schedule matches */
  277.  
  278. if pos("AUTOMATIC",scht) > 0 then do
  279.    nums = ''
  280.    numct= 0
  281.    closed = tcount
  282.    rndseed= tcount * rndcnt
  283.    do while closed ~= 0
  284.       t = random(1,closed,time('s'))
  285.       if find(nums,t) = 0 then do
  286.          if numct = 0 then do
  287.             numct = 1
  288.             nums  = t
  289.          end
  290.          else do
  291.             numct = numct + 1
  292.             nums = nums||" "||t
  293.          end
  294.       end
  295.       if numct = tcount-1 then do
  296.          do f=1 to tcount
  297.             if pos(f,nums) = 0 then
  298.                nums = nums||" "f
  299.          end
  300.          numct = numct + 1
  301.          closed = 0
  302.       end
  303.    end
  304. end
  305.  
  306. if open(outfile,league_file || input2_file,"w") then do
  307.    writeln(outfile,"*")
  308.    writeln(outfile,"**" cupname)
  309.    writeln(outfile,"*")
  310.    writeln(outfile,"*")
  311.    if pos("2 Legs",rnds.1) > 0 then
  312.       writeln(outfile,"*Round="word(rnds.1,1)"   1 Leg")
  313.    else
  314.       writeln(outfile,"*Round="word(rnds.1,1))
  315.    writeln(outfile,"*")
  316.    if pos("AUTOMATIC",scht) > 0 then do
  317.       home = 0
  318.       away = 0
  319.       do i=1 to tcount by 2
  320.          home = word(nums,i)
  321.          away = word(nums,i+1)
  322.          writech(outfile,left(teams.home,30))
  323.          writeln(outfile," __   __ " teams.away)
  324.       end
  325.       writech(outfile,"*")
  326.    end
  327.    close(outfile)
  328. end
  329. else do
  330.    say
  331.    say cs_error
  332.    say
  333.    say cs_t2"'"league_file || input2_file"'."
  334.    exit
  335. end
  336.  
  337. matches = 0
  338. left    = tcount
  339.  
  340. do while left ~= 1
  341.    matches = matches + (left/2)
  342.    left = left / 2
  343. end
  344. if pos("YES",third) > 0 then
  345.    matches = matches + 1
  346.  
  347. do i=1 to rndcnt                    /* amend count if two legs */
  348.    if pos("2 Legs",rnds.i) > 0 then
  349.       matches = matches + 1
  350. end
  351.  
  352. say
  353. say center(cs_t8"'"cupname"'",78)
  354. say "-------------------------------------------------------------------------------"
  355. say "                                               "cs_t9""date('e')""cs_t10""time()
  356. say
  357. say cs_t11""fversion
  358. say "_______________________________________________________________________________"
  359. say
  360. say cs_t12""left(tcount,3)""cs_t14""matches
  361. say "                               "cs_t15
  362. say
  363. if pos("MANUAL",scht) > 0 then
  364.    say cs_t16
  365. else
  366.    say cs_t17
  367. if pos("YES",third) > 0 then
  368.    say cs_t18
  369. if pos("YES",cuph) > 0 then
  370.    say cs_t19
  371. if pos("YES",awaygoals) > 0 then do
  372.    say cs_t20
  373.    say cs_t21
  374. end
  375. say
  376. say "'"cupname"'"cs_t22""rndcnt""cs_t23
  377. say cs_t24
  378. say
  379. say cs_t25
  380. cs_line = ""
  381. do cs_l=1 to length(cs_t25)+5
  382.    cs_line = cs_line||"-"
  383. end
  384. say cs_line
  385. say
  386. do i=1 to rndcnt
  387.    rndmsg = ""
  388.    if pos("1st",rnds.i) > 0 then rndmsg = rndmsg||left(strip(cs_t39)" "cs_t28,23)
  389.    if pos("2nd",rnds.i) > 0 then rndmsg = rndmsg||left(strip(cs_t40)" "cs_t28,23)
  390.    if pos("3rd",rnds.i) > 0 then rndmsg = rndmsg||left(strip(cs_t41)" "cs_t28,23)
  391.    if pos("4th",rnds.i) > 0 then rndmsg = rndmsg||left(strip(cs_t42)" "cs_t28,23)
  392.    if pos("5th",rnds.i) > 0 then rndmsg = rndmsg||left(strip(cs_t43)" "cs_t28,23)
  393.  
  394.    if pos("Quart",rnds.i) > 0 then rndmsg = rndmsg||left(strip(cs_t35),23)
  395.    if pos("Semi",rnds.i) > 0 then rndmsg = rndmsg||left(strip(cs_t33),23)
  396.    if pos("Third",rnds.i) > 0 then rndmsg = rndmsg||left(strip(cs_t36),23)
  397.    if pos("Final",rnds.i) > 0 then rndmsg = rndmsg||left(strip(cs_t30),23)
  398.  
  399.    if pos("No Ext.Time",rnds.i) > 0 then rndmsg = rndmsg||left(strip(fc_t3),19)
  400.    if pos("E.T If Rep.",rnds.i) > 0 then rndmsg = rndmsg||left(strip(fc_t4),19)
  401.    if pos("Extra Time",rnds.i) > 0 then rndmsg = rndmsg||left(strip(fc_t5),19)
  402.  
  403.    if pos("No Pens.",rnds.i) > 0 then rndmsg = rndmsg||left(strip(fc_t6),21)
  404.    if pos("Pen If Rep.",rnds.i) > 0 then rndmsg = rndmsg||left(strip(fc_t7),21)
  405.    if pos("Penalties",rnds.i) > 0 then rndmsg = rndmsg||left(strip(fc_t8),21)
  406.  
  407.    if pos("2 Legs",rnds.i) > 0 then rndmsg = rndmsg||fc_t2
  408.    say rndmsg
  409. end
  410.  
  411. /*
  412.    if pos("1 Leg",rnds.i) > 0 then do
  413.       k = pos("1 Leg",rnds.i)
  414.       rnds.i = overlay("      ",rnds.i,k)
  415.       say rnds.i
  416.    end
  417.    else
  418.       say rnds.i
  419. */
  420. say
  421. say "_______________________________________________________________________________"
  422. say
  423. say cs_t26
  424. say cs_t27
  425. say
  426. do i=1 to tcount
  427.    say teams.i
  428. end
  429. say
  430. say "_______________________________________________________________________________"
  431. say
  432. if pos("2 Legs",rnds.1) > 0 then
  433.    say cs_t28": "bettername(word(rnds.1,1),"1 Leg")
  434. else
  435.    say cs_t28": "bettername(word(rnds.1,1),cs_t29)
  436. say "------------------------------------"
  437. say
  438. if pos("AUTOMATIC",scht) > 0 then do
  439.    home = 0
  440.    away = 0
  441.    do i=1 to tcount by 2
  442.       home = word(nums,i)
  443.       away = word(nums,i+1)
  444.       say left(teams.home,30)" __   __  "teams.away
  445.    end
  446.    say
  447.    say "-------------------------------------------------------------------------------"
  448.    say
  449. end
  450.  
  451. exit
  452.  
  453. /* Routine ----------------------------------------------------------- */
  454.  
  455. bettername:
  456. parse arg crn,legless
  457.  
  458. trdn = strip(substr(crn,1,1))
  459. if datatype(trdn,'n') = 1 then do
  460.    parse var crn roundno" "extra
  461.    trn = strip(crn)" "cs_t28" "extra
  462. end
  463. else do
  464.    if pos(cs_t30,crn) > 0 then do
  465.       parse var crn . " "extra
  466.       if pos(cs_t31,crn) > 0 then
  467.          trn = cs_t30" "extra
  468.       else
  469.          trn = cs_t30
  470.    end
  471.    if pos(strip(cs_t45),crn) > 0 then do
  472.       parse var crn . " "extra
  473.       if pos(cs_t31,crn) > 0 then
  474.          trn = cs_t32" "extra
  475.       else
  476.          trn = cs_t33
  477.    end
  478.    if pos(strip(cs_t44),crn) > 0 then do
  479.       parse var crn . " "extra
  480.       if pos(cs_t31,crn) > 0 then
  481.          trn = cs_t34" "extra
  482.       else
  483.          trn = cs_t35
  484.    end
  485.    if pos(strip(cs_t46),crn) > 0 then do
  486.       trn = cs_t36
  487.    end
  488. end
  489.  
  490. if pos("1 Leg",legless) > 0 then
  491.    trn = trn||"  ("cum_t32")"
  492. if pos("2 Legs",legless) > 0 then
  493.    trn = trn||"  ("cum_t33")"
  494.  
  495. return trn
  496.  
  497. /* ------------------------------------------------------------------- */